<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- 用户名订单号查询有误 -->
      <el-input v-model="orderReqDto.appName" placeholder="应用名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="应用编号" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应用名称" align="center" width="160">
        <template slot-scope="{row}">
          <span>{{ row.appName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="code" align="center" min-width="120">
        <template slot-scope="{row}">
          <span>{{ row.codes }}</span>
        </template>
      </el-table-column>
      <el-table-column label="查询Url" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.queryUrl }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录Url" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.loginUrl }}</span>
        </template>
      </el-table-column>
      <el-table-column label="秘钥" align="center" min-width="200">
        <template slot-scope="{row}">
          <span>{{ row.merchantKey }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="300px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="orderReqDto.current"
      :limit.sync="orderReqDto.size"
      @pagination="saleQuantityList"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="orderReqDto" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">

        <el-form-item label="code">
          <el-input v-model="orderReqDto.codes" />
        </el-form-item>
        <el-form-item label="查询URL">
          <el-input v-model="orderReqDto.queryUrl" />
        </el-form-item>
        <el-form-item label="登录URL" prop="account">
          <el-input v-model="orderReqDto.loginUrl" />
        </el-form-item>
        <el-form-item label="秘钥">
          <el-input v-model="orderReqDto.merchantKey" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel(1)">
          取消
        </el-button>
        <el-button type="primary" @click="updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { saleQuantityList, saleQuantityUpdate } from '@/api/order'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Orderlist',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableData: [],
      times: [],
      total: 0,
      tableKey: 0,
      orderReqDto: {
        size: 10,
        current: 1
      },
      countOrder: {},
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        username: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      listLoading: false,
      orderNoForDetail: '',
      optionsCopy: [],
      options: [],
      priceoptionsCopy: [],
      priceoptions: [],
      value: ''
    }
  },
  created() {
    this.saleQuantityList(this.orderReqDto)
  },
  methods: {
    cancel(type) {
      if (type === 1) {
        this.dialogFormVisible = false
        this.saleQuantityList()
        this.orderReqDto = {}
      }
    },
    handleUpdate(row) {
      this.orderReqDto = row
      this.orderReqDto.roles = this.roles
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    async updateData() {
      const res = await saleQuantityUpdate(this.orderReqDto)
      if (res.data.code === 200) {
        this.$notify({
          title: 'success',
          message: 'update success',
          type: 'success',
          duration: 2000
        })
        this.saleQuantityList()
      } else {
        this.$notify({
          title: 'fail',
          message: 'update fail',
          type: 'fail',
          duration: 2000
        })
      }
      this.dialogFormVisible = false
    },
    // 获取订单列表
    async saleQuantityList(orderReqDto) {
      const res = await saleQuantityList(this.orderReqDto)
      if (res.data.data.records === null) {
        this.tableData = null
      } else {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      }
    },

    handleFilter() {
      this.orderReqDto.current = 1
      if (this.times == null) {
        this.orderReqDto.endTime = ''
        this.orderReqDto.startTime = ''
      } else if (this.times.length === 0) {
        this.orderReqDto.endTime = ''
        this.orderReqDto.startTime = ''
      } else {
        this.orderReqDto.endTime = this.times[1]
        this.orderReqDto.startTime = this.times[0]
      }
      this.saleQuantityList(this.orderReqDto)
    }
  }
}
</script>

<style scoped>

</style>
